package com.gitee.kinbug.sniper.web.data;

import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.gitee.kinbug.sniper.common.entity.Bar;
import com.gitee.kinbug.sniper.common.enums.CandleEnum;
import com.gitee.kinbug.sniper.common.enums.SymbolEnum;
import com.gitee.kinbug.sniper.common.util.KlineUtil;
import com.gitee.kinbug.sniper.web.device.Index;
import com.gitee.kinbug.sniper.web.util.BeanCopyUtil;
import com.google.common.collect.Lists;
import com.huobi.wss.event.MarketKLineReqResponse;
import com.huobi.wss.event.MarketKLineSubResponse;
import com.huobi.wss.handle.WssMarketHandle;
import com.huobi.wss.handle.WssMarketReqHandle;
import com.huobi.wss.request.KLineSubRequest;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RealTimeData {

	private String URL;

	private int barNum;

	private Index index;

	private WssMarketHandle wssMarketHandle;

	public RealTimeData(String URL, Index index) {
		this.URL = URL;
		this.barNum = 100;
		this.index = index;
		this.wssMarketHandle = new WssMarketHandle(URL);
	}

	private void kLineRequest(SymbolEnum symbolEnum, CandleEnum candleEnum) {
		try {
			WssMarketReqHandle wssMarketReqHandle = new WssMarketReqHandle(URL, response -> {
				MarketKLineReqResponse mKline = JSON.parseObject(response, MarketKLineReqResponse.class);
				// List数据到模型
				index.addBar(mKline.getData().stream().map(dataBean -> BeanCopyUtil.dataBeanToBar(dataBean, symbolEnum, candleEnum)).collect(Collectors.toList()));
			});
			long end = KlineUtil.getKlineId(candleEnum, System.currentTimeMillis());
			long start = KlineUtil.getAgoKlineId(candleEnum, end, barNum);
			KLineSubRequest request = KLineSubRequest.builder().req(symbolEnum.getMarketMsg(candleEnum)).from(start / 1000).to(end / 1000).build();
			wssMarketReqHandle.doReq(JSON.toJSONString(request));
		} catch (Exception e) {
			log.error("获取历史k线异常：" + e);
		}
	}

	private void subMarket(SymbolEnum symbolEnum, List<CandleEnum> candles) {
		try {
			List<String> channels = Lists.newArrayList();
			for (CandleEnum candleEnum : candles) {
				channels.add(symbolEnum.getMarketMsg(candleEnum));
				channels.add(symbolEnum.getMarketMsg(candleEnum));
				channels.add(symbolEnum.getMarketMsg(candleEnum));
				channels.add(symbolEnum.getMarketMsg(candleEnum));
			}
			wssMarketHandle.sub(channels, response -> {
				MarketKLineSubResponse event = JSON.parseObject(response, MarketKLineSubResponse.class);
				Bar bar = BeanCopyUtil.tickBeanToBar(event.getTick(), SymbolEnum.offer(event.getCh()), CandleEnum.offer(event.getCh()));
				index.addBar(bar);
			});

			Thread.sleep(Long.MAX_VALUE);
		} catch (Exception e) {
			log.error("订阅行情异常：" + e);
		}
	}

	/**
	 * -初始化消息Bar -订阅实时消息Bar
	 * 
	 * @param symbol           币队
	 * @param List<CandleEnum> candles 订阅k线集
	 */
	public void cxecute(SymbolEnum symbol, List<CandleEnum> candles) {
		for (CandleEnum candleEnum : candles) {
			this.kLineRequest(symbol, candleEnum);
		}
		this.subMarket(symbol, candles);
	}
}
